﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web.Security;

namespace WebshopPizzaRio
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private FileUpload fileImage;
        private Label Label1;
        private Label Label2;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (Context.User.IsInRole("Administrators"))
                {
                    RegularExpressionValidator1.Enabled = false;
                    RequiredFieldValidator2.Enabled = false;
                    RequiredFieldValidator1.Enabled = false;
                    TextBox1.Enabled = false;
                    TextBox2.Enabled = false;
                    Button1.Enabled = false;

                    FormView MyFormView = (FormView)LoginView1.FindControl("FormView1");
                    fileImage = (FileUpload)MyFormView.FindControl("fileImage");
                    Label1 = (Label)MyFormView.FindControl("Label1");
                    Label2 = (Label)MyFormView.FindControl("Label2");
                }
            }
        }

        protected void valFile_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (!fileImage.HasFile)
            {
                args.IsValid = false;
            }
        }

        protected void valFileType_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (fileImage.HasFile)
            {
                string[] acceptedTypes = new string[] 
        { 
            "image/bmp", 
            "image/jpeg", 
            "image/tiff", 
            "image/gif", 
            "image/png" 
        };

                if (!acceptedTypes.Contains(fileImage.PostedFile.ContentType))
                {
                    args.IsValid = false;
                }
            }
        }

        protected void valFileSize_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (IsValid)
            {
                int maxSize = 5 * 1024 * 1024;
                if (fileImage.PostedFile.ContentLength > maxSize)
                {
                    args.IsValid = false;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                Bitmap image = ResizeImage(fileImage.PostedFile.InputStream, 200, 400);
                String guid = Guid.NewGuid().ToString();
                String link = "&lt;img src=\"/Images/Voorpagina/" + guid + ".jpg" + "\"&#47;&#62;";
                image.Save(Server.MapPath("~/Images/Voorpagina/") + guid + ".jpg", ImageFormat.Jpeg);
                Label1.Text = link;
                Label2.Visible = true;
                Label1.Visible = true;
            }
        }

        private Bitmap ResizeImage(Stream streamImage, int maxWidth, int maxHeight)
        {
            Bitmap originalImage = new Bitmap(streamImage);
            int newWidth = originalImage.Width;
            int newHeight = originalImage.Height;
            double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;

            if (aspectRatio <= 1 && originalImage.Width > maxWidth)
            {
                newWidth = maxWidth;
                newHeight = (int)Math.Round(newWidth / aspectRatio);
            }
            else if (aspectRatio > 1 && originalImage.Height > maxHeight)
            {
                newHeight = maxHeight;
                newWidth = (int)Math.Round(newHeight * aspectRatio);
            }

            return new Bitmap(originalImage, newWidth, newHeight);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            NieuwsbriefBLL nb = new NieuwsbriefBLL();
            nb.insertNieuwsBriefToevoegen(TextBox1.Text, TextBox2.Text);
            Label3.Visible=true;
        }
    }
}